<script setup>
import {ref} from 'vue'
import axios from 'axios'
// 数据
const dataList = ref([])
const ClickList =async ()=>{
  const res = await axios.get('/api/users')
  dataList.value = res.data.data
}
ClickList()
// 表单元素
const ruleFormRef = ref(null)
// 表单数据
const ruleForm = ref({name:"",dian:"",title:"",city:"",date:""})

// 弹出窗的开启
const drawer = ref(false)
// 控制弹窗框的标题
const title_ = ref('')

// 获取某一列的id
const RowId = ref()
// 修改数据
const handleEdit = (id)=>{
  drawer.value = true
  title_.value = '修改数据'
  const {name,dian,title,city,date} = dataList.value.find((item)=>item.id==id)
  RowId.value = id
  ruleForm.value = {name,dian,title,city,date}
}
// 添加数据
const handleAdd = ()=>{
  drawer.value = true
  title_.value = '添加数据'
}
// 删除数据
const handleDelete =async (id)=>{
  await axios.delete(`/api/users/`+id)
  ClickList()
  ElMessage.success('删除成功')
}

// 弹窗确定
const sure = async()=>{
  drawer.value = false
  if(title_.value==='添加数据'){
     await axios.post('/api/users',ruleForm.value)
  }else{
    await axios.patch(`/api/users/${RowId.value}`,ruleForm.value)
  }
  ClickList()
  ruleForm.value = {name:"",dian:"",title:"",city:"",date:""}
  ElMessage.success(title_.value==='添加数据'?'添加成功':'修改成功')
}
// 弹窗取消
const cancel = ()=>{
  drawer.value = false
  ElMessage.error('已取消')
  ruleForm.value = {name:"",dian:"",title:"",city:"",date:""}
}
</script>
<template>
   <el-table :data="dataList" stripe style="width: 100%">
      <el-table-column label="持有者" width="120">
        <template #default="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column property="dian" label="店类型" width="120" />
      <el-table-column
        property="title"
        label="店内介绍"
        width="240"
        show-overflow-tooltip
      />
      <el-table-column property="city" label="店地址" />
      <el-table-column property="date" label="开店时间" />
      <el-table-column label="修改信息">
      <template #default="scope">
        <el-button size="small" type="success" @click="handleEdit(scope.row.id)">
          修改数据
        </el-button>
        <el-button size="small" type="primary" @click="handleAdd()">
          添加数据
        </el-button>
        <el-button size="small" type="danger" @click="handleDelete(scope.row.id)">
          删除数据
        </el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-drawer v-model="drawer" :title="title_" :close-on-click-modal="false" :show-close="false">
    <!-- 表单 -->
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      status-icon
      label-width="auto"
      class="demo-ruleForm"
    >
      <el-form-item label="持有者">
        <el-input v-model="ruleForm.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="店类型">
        <el-input v-model="ruleForm.dian" autocomplete="off" />
      </el-form-item>
      <el-form-item label="店内介绍">
        <el-input v-model.number="ruleForm.title" />
      </el-form-item>
      <el-form-item label="店地址">
        <el-input v-model.number="ruleForm.city" />
      </el-form-item>
      <el-form-item label="开店时间">
        <el-input v-model.number="ruleForm.date" />
      </el-form-item>
    </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel()">取消</el-button>
          <el-button type="primary" @click="sure()">确定</el-button>
        </div>
      </template>
  </el-drawer>
</template>
<style scoped lang='scss'>
</style>
